package dao;

import java.util.ArrayList;

import org.hibernate.Session;


import metier.Encherir;
 

public class DAOEncherirHib extends DAOHibernate implements DAOEncherir{

	@Override
	public Encherir get(int id) throws Exception {
		Encherir	encherir = null;
		Session	session = connect();		
		encherir = (Encherir) session.get(Encherir.class, id);	
		close(session);
		return encherir;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Encherir> loadAll() throws Exception {
		ArrayList<Encherir>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Encherir>) session.createQuery("FROM Encherir")
				.list();
		
		close(session);
		return tab;
	}

	@Override
	public void save(Encherir encherir) throws Exception {
		Session	session = connect();
		
		session.save(encherir);
		
		close(session);	
		
	}

	@Override
	public void saveOrupdate(Encherir encherir) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Encherir encherir) throws Exception {
		Session	session = connect();
		
		session.delete(encherir);
		encherir.setId_personne(-1);		
		close(session);
		
		
	}

	@Override
	public void update(Encherir encherir) throws Exception {
		Session	session = connect();
		
		session.update(encherir);
		
		close(session);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Encherir> allEnchere(int id_offre) throws Exception {
		ArrayList<Encherir>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Encherir>) session.createQuery("FROM Encherir where id_offre LIKE '" +id_offre+"'")
				.list();
		
		close(session);
		return tab;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Encherir lastEnchere(int id_offre) throws Exception {
		ArrayList<Encherir>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Encherir>) session.createQuery("FROM Encherir where id_offre LIKE '" +id_offre+"'")
				.list();
		
		
		close(session);
		return tab.get(tab.size()-1);//size > 0
	}

}
